package com.cqqec.boot.controller;

import com.cqqec.boot.entity.User;
import com.cqqec.boot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 登录页面的请求
     * @return
     */
    @RequestMapping(value = {"/" , "/login.html"})
    public String login(){
        return "login";
    }
    @PostMapping("/login")//和表单进行对应
    //获取前端表单提交的数据 用户名和密码
    public String login(@RequestParam String username, @RequestParam String password, Model model) {
        System.out.println("加密前：" + password);
        //加密
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        System.out.println("加密后：" + password);
        //根据用户名在前端页面输入的用户名，去数据库查询用户信息
        User user = userService.getUsername(username);
        if (user == null) {
            model.addAttribute("msg", "用户不存在");
            return "login";
        }
        if (!user.getPassword().equals(password)) {
            model.addAttribute("msg", "密码错误");
            return "login";
        }
        //登录成功  跳转地址
        return "redirect:/getList/1";
    }

}
